package com.fenghongzhang;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.fenghongzhang.day008.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
    }

    public void write(View view) {
        try {
            FileOutputStream fileOutputStream = openFileOutput("one.txt", MODE_PRIVATE);
            fileOutputStream.write("hehe".getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void read(View view) {
        try {
            FileInputStream fileInputStream = openFileInput("one.txt");

            byte[] b = new byte[1024];
            int len = 0;
            while((len=fileInputStream.read(b)) != -1){
                String s = new String(b, 0, len);
                Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            }
            fileInputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
